package thesis.filesharing.common.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import thesis.filesharing.common.exception.system.AppSystemException;

public class XMLUtil {
	
	public static List<String> readDataNodes(String fileName, String tagName) {
		try {
			File file = new File(fileName);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = factory.newDocumentBuilder();
			Document doc = documentBuilder.parse(file);
			doc.getDocumentElement().normalize();
			NodeList nodeList = doc.getElementsByTagName(tagName);
			
			Node node = null;
			List<String> data = new ArrayList<String>();
			for (int i = 0; i < nodeList.getLength(); i++) {
				node = nodeList.item(i);
				Element e = (Element) node;
				data.add(e.getTextContent());
			}
			
			return data;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}
}
